home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / scrasm / keyb.inc < prev    next >
Encoding:
Text File  |  1993-03-09  |  9.0 KB  |  237 lines

  1. ;; ====================================================================
  2. ;; Macros
  3. ;; ====================================================================
  4. ;; Jump if key pressed
  5. JKEYP           MACRO   key,label
  6.                 cmp     byte ptr cs:_keyFlags[key+1],1
  7.                 je      label
  8.                 ENDM
  9. ;; Jump if key not pressed
  10. JKEYNP          MACRO   key,label
  11.                 cmp     byte ptr cs:_keyFlags[key+1],1
  12.                 jne     label
  13.                 ENDM
  14.  
  15. ;; Note that JNKEY and JKEY both modify _flKeyChanged, so you cannot
  16. ;; use one after the other!  In other words,
  17. ;;  JKEYNP no_key
  18. ;;  JKEYP  yes_key      ;<-- this will fail
  19. ;; will not work like you'd think it would.  The second call (JKEYP)
  20. ;; will not know that a key has been pressed!
  21. ;; Jump if no key pressed:
  22. JNKEY           MACRO   label
  23.                 cmp     cs:_flKeyChanged,0
  24.                 je      label
  25.                 mov     cs:_flKeyChanged,0      ; <--- important!
  26.                 ENDM
  27. ;; Jump if key pressed:
  28. JKEY            MACRO   label
  29.                 cmp     cs:_flKeyChanged,0
  30.                 mov     cs:_flKeyChanged,0
  31.                 jne     label
  32.                 ENDM
  33.  
  34. ;; Start keyboard interrupts
  35. KEYB_START      MACRO
  36.                 call    SwapInt9
  37.                 mov     cs:_flKeyChanged,0
  38.                 ENDM
  39.  
  40. ;; Clear keyboard interrupts
  41. KEYB_END        MACRO
  42.                 call    SwapInt9
  43.                 ENDM
  44.  
  45. ;; Credit for these routines:  Steve Dollins, Brown Computer Group.
  46. ;; I didn't write any of the code below -- just heisted it from some
  47. ;; stuff that he wrote and released!  Very useful keyboard routines.
  48. ;; Any comments prefixed SDE were added by me.
  49. _keyFlags       dw      256 dup (0)     ; SDE: since they only use 2 bits
  50.                                         ; per word, this is a tradeoff,
  51.                                         ; space for time
  52.  
  53. oldint9_offset  dw      offset newint9
  54. oldint9_segment dw      seg newint9
  55.  
  56. _flKeyChanged   dw      0
  57.  
  58. ;-----------------------------------------------------------------------
  59. ; void SwapInt9( void )
  60. ;
  61. ;       SwapInt9() exchanges the vector in oldint9_segment:oldint9_offset
  62. ;       with the vector in the interrupt table for INT 9h.
  63. ;-----------------------------------------------------------------------
  64.  
  65. SwapInt9        PROC    far
  66.                 mov     ax,cs
  67.                 mov     ds,ax
  68.  
  69.                 mov     ax,03509h       ; Get interrupt 09h
  70.                 int     21h             ;   return in ES:BX
  71.  
  72.                 mov     ax,oldint9_segment
  73.                 mov     dx,oldint9_offset
  74.                 push    ds
  75.                 mov     ds,ax
  76.                 mov     ax,02509h       ; Set new interrupt
  77.                 int     21h             ;  to address in DS:DX
  78.                 pop     ds
  79.  
  80.                 mov     oldint9_segment,es    ; Save the old interrupt
  81.                 mov     oldint9_offset,bx
  82.                 ret
  83. SwapInt9        ENDP
  84.  
  85.  
  86. ;-----------------------------------------------------------------------
  87. ; newint9 is the new keyboard interrupt (INT 9h).
  88. ;
  89. ;       Reads the scan code from the keyboard and modifies the key
  90. ;       flags table.  The high byte is set to the position of the key,
  91. ;       pressed=1, release=0.  The low byte is set to 1 when the key
  92. ;       is pressed and left unmodified when the key is released.
  93. ;-----------------------------------------------------------------------
  94. newint9         PROC    far
  95.                 push    ax
  96.                 push    bx
  97.                 push    ds
  98.  
  99.                 mov     ax,cs
  100.                 mov     ds,ax
  101.  
  102.                 JKEYNP  kCTRL,not_ctrlaltdel    ; SDE code
  103.                 JKEYNP  kALT,not_ctrlaltdel     ; To still allow ctrl-
  104.                 JKEYNP  kDELETE,not_ctrlaltdel  ; alt-delete.  Nothing
  105.                 jmp     ctrlaltdel      ; worse than a total lockup!
  106. not_ctrlaltdel:
  107.  
  108.                 in      ax,60h          ; get scan code in AL, control byte in AH
  109.                 mov     bx,ax           ; save a copy in BX
  110.                 xchg    ah,al           ; swap to get control byte in AL
  111.                 or      al,80h          ; clear keyboard
  112.                 out     61h,al          ;   of interrupt
  113.                 and     al,7Fh
  114.                 out     61h,al
  115.                 mov     al,20h          ; send generic EOI to
  116.                 out     20h,al          ;   PIC
  117.  
  118.                 and     bx,0007fh       ; strip all but the scan code
  119.                 shl     bx,1            ; multiply by two to get our offset
  120.  
  121.                 ; if the key was released, the high bit is set in the scan code
  122.                 bt      ax,15           ; move this high bit into the carry flag
  123.                 setnc   byte ptr [_keyFlags+bx+1] ; set "Is being pressed" flag
  124.                 jc      short int09done ; if the key was released, we're done
  125.                 mov     byte ptr [_keyFlags+bx],1 ; set "Has been pressed" flag
  126.                 mov     _flKeyChanged,1         ; state of keyboard has changed
  127. int09done:
  128.                 mov     _flKeyChanged,1         ; state of keyboard has changed
  129.                 pop     ds
  130.                 pop     bx
  131.                 pop     ax
  132.                 iret
  133. ctrlaltdel:     int     19h                     ; SDE -- added this.
  134.                                                 ;  Allows a reboot.
  135. newint9         ENDP
  136.  
  137. ;; Defines the current key procedure (used as a jump-through)
  138. kprocCur        dw      KprocDirect
  139.  
  140. ;; This is a keyboard procedure.  Normally, this would control some
  141. ;; sprite, or something, and the screen would follow the sprite.  For
  142. ;; the purposes of this code, though (namely, sprite-less scrolling)
  143. ;; it just directly affects ScrollDX and ScrollDY.
  144. ;; This keyproc is inertialess, use + and - to increase speed and
  145. ;; the up/down/left/right keys to move directions.
  146. ;; Pressing K will switch to the other keyprocedure on the fly.
  147. ;; P pauses the screen -- note that this is just for completely
  148. ;; freezing the screen... it doesn't return until you let go!
  149.  
  150. EVEN
  151. scroll_speed_x  dw      SCROLL_SPEED                    ; (defaults)
  152. scroll_speed_y  dw      SCROLL_SPEED * VIRTUAL_WIDTH    ; (defaults)
  153. KprocDirect     PROC    near
  154. chk_leftright:  mov     ax,0
  155.                 JKEYNP  kRIGHT,not_right
  156.                 mov     ax,scroll_speed_x
  157.                 mov     ScrollDX,ax
  158.                 jmp     chk_updown
  159. not_right:      JKEYNP  kLEFT,not_left
  160.                 sub     ax,scroll_speed_x
  161.                 mov     ScrollDX,ax
  162.                 jmp     chk_updown
  163. not_left:       mov     ScrollDX,ax
  164.  
  165. chk_updown:     mov     ax,0
  166.                 JKEYNP  kUP,not_up
  167.                 sub     ax,scroll_speed_y
  168.                 mov     ScrollDY,ax
  169.                 jmp     chk_other
  170. not_up:         JKEYNP  kDOWN,not_down
  171.                 mov     ax,scroll_speed_y
  172.                 mov     ScrollDY,ax
  173.                 jmp     chk_other
  174. not_down:       mov     ScrollDY,ax
  175.  
  176. chk_other:      JKEYNP  kK,not_k
  177.                 mov     kprocCur,KprocInertia
  178. not_k:          JKEYNP  kM,not_m
  179.                 mov     bDoTransition,1
  180. not_m:          JKEYNP  kGREY_MINUS,not_minus
  181.                 cmp     scroll_speed_x,1
  182.                 jle     not_minus
  183.                 dec     scroll_speed_x
  184.                 sub     scroll_speed_y,VIRTUAL_WIDTH
  185. not_minus:      JKEYNP  kGREY_PLUS,not_plus
  186.                 cmp     scroll_speed_x,16
  187.                 jge     not_plus
  188.                 inc     scroll_speed_x
  189.                 add     scroll_speed_y,VIRTUAL_WIDTH
  190. not_plus:
  191.  
  192. pause_key:      JKEYP   kP,pause_key
  193.  
  194.                 ret
  195. KprocDirect     ENDP
  196.  
  197. ;; This keyproc has inertia, so + and - don't work.
  198. ;; Use up/down/left/right keys to increase speed in those directions.
  199. ;; Pressing K will switch to the other keyprocedure on the fly.
  200. ;; P pauses the screen -- note that this is just for completely
  201. ;; freezing the screen... it doesn't return until you let go!
  202. KprocInertia    PROC    near
  203. chk2_leftright: JKEYNP  kRIGHT,not2_right
  204.                 cmp     ScrollDX,16
  205.                 je      not2_right
  206.                 inc     ScrollDX
  207.                 jmp     chk2_updown
  208. not2_right:     JKEYNP  kLEFT,not2_left
  209.                 cmp     ScrollDX,-16
  210.                 je      not2_left
  211.                 dec     ScrollDX
  212.                 jmp     chk2_updown
  213. not2_left:
  214.  
  215. chk2_updown:    JKEYNP  kUP,not2_up
  216.                 cmp     ScrollDY,-VIRTUAL_WIDTH * 16
  217.                 je      not2_up
  218.                 add     ScrollDY,-VIRTUAL_WIDTH
  219.                 jmp     chk2_other
  220. not2_up:        JKEYNP  kDOWN,not2_down
  221.                 cmp     ScrollDY,VIRTUAL_WIDTH * 16
  222.                 je      not2_down
  223.                 add     ScrollDY,VIRTUAL_WIDTH
  224.                 jmp     chk2_other
  225. not2_down:
  226.  
  227. chk2_other:     JKEYNP  kK,not2_k
  228.                 mov     kprocCur,KprocDirect
  229. not2_k:         JKEYNP  kM,not2_m
  230.                 mov     bDoTransition,1
  231. not2_m:
  232.  
  233. pause2_key:     JKEYP   kP,pause2_key
  234.  
  235.                 ret
  236. KprocInertia    ENDP
  237.